﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Vlc.DotNet.Forms;
using Vlc.DotNet.Core;
using Vlc.DotNet.Core.Interops;
using System.IO;

namespace DnPakExplorer.Forms {
	public partial class VideoForm : Form {

		VlcControl mControl;

		public VideoForm() {
			mControl = new VlcControl();
			mControl.BeginInit();
			mControl.VlcLibDirectory = new System.IO.DirectoryInfo(@"..\..\Vlc.DotNet-master\Vlc.DotNet-master\lib\x86");
			mControl.EndInit();
			mControl.Dock = DockStyle.Fill;
			mControl.Stopped +=mControl_Stopped;
			mControl.Playing +=mControl_Playing;
			mControl.MediaChanged +=mControl_MediaChanged;
			mControl.Opening += mControl_Opening;
			mControl.PausableChanged +=mControl_PausableChanged;
			mControl.Paused +=mControl_Paused;
			mControl.PositionChanged +=mControl_PositionChanged;
			mControl.Buffering +=mControl_Buffering;
			mControl.ChangeUICues +=mControl_ChangeUICues;
			mControl.EncounteredError +=mControl_EncounteredError;
			InitializeComponent();
			
			this.splitContainer1.Panel1.Controls.Add(mControl);
			this.ResumeLayout();
		}

		void mControl_EncounteredError(object sender, VlcMediaPlayerEncounteredErrorEventArgs e) {
			MessageBox.Show("Error:"+e.ToString());
		}

		void mControl_ChangeUICues(object sender, UICuesEventArgs e) {
		}

		void mControl_Buffering(object sender, VlcMediaPlayerBufferingEventArgs e) {
		}

		void mControl_PositionChanged(object sender, VlcMediaPlayerPositionChangedEventArgs e) {
		
		}

		void mControl_Paused(object sender, VlcMediaPlayerPausedEventArgs e) {
			button1.InvokeIfRequired(l => button1.Text="Play");
		}

		void mControl_PausableChanged(object sender, VlcMediaPlayerPausableChangedEventArgs e) {
		}

		void mControl_Opening(object sender, VlcMediaPlayerOpeningEventArgs e) {
			
		}

		void mControl_MediaChanged(object sender, VlcMediaPlayerMediaChangedEventArgs e) {
		}

		void mControl_Playing(object sender, Vlc.DotNet.Core.VlcMediaPlayerPlayingEventArgs e) {
			button1.InvokeIfRequired(l => button1.Text="Stop");
		}

		void mControl_Stopped(object sender, Vlc.DotNet.Core.VlcMediaPlayerStoppedEventArgs e) {
			button1.InvokeIfRequired(l => button1.Text="Play");
		}

		private void OnBtnPlay(object sender, EventArgs e) {
			if (!mControl.IsPlaying) {
				mControl.Play();
			} else {
				mControl.Stop();
			}
		}

		private void OnBtnFile(object sender, EventArgs e) {
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.Multiselect = false;
			dlg.CheckFileExists = true;
			if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
				if (mControl.IsPlaying)
					mControl.Stop();
				mControl.Play(new System.IO.FileInfo(dlg.FileName));
				this.Text = Path.GetFileName(dlg.FileName);
			}
		}

		public void SetFile(FileInfo file) {
			if (mControl.IsPlaying)
				mControl.Stop();
			mControl.Play(file);
			this.Text = file.Name;
		}
	}
}
